package spring_learn.a15;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.context.annotation.*;
import org.springframework.context.support.GenericApplicationContext;
import sun.jvmstat.perfdata.monitor.PerfStringVariableMonitor;


//验证代理是否可以被重复代理
public class A15_1 {

    public static void main(String[] args) {
        GenericApplicationContext genericApplicationContext=new GenericApplicationContext();
        genericApplicationContext.registerBean(config.class);
        genericApplicationContext.registerBean(ConfigurationClassPostProcessor.class);
        genericApplicationContext.registerBean(MyAdvice.class);
        genericApplicationContext.registerBean(AnnotationAwareAspectJAutoProxyCreator.class);
        genericApplicationContext.refresh();

        Bean1 bean1=genericApplicationContext.getBean(Bean1.class);
        bean1.foo();
        System.out.println(bean1.getClass());
        System.out.println(genericApplicationContext.getBean("scopedTarget.bean1").getClass());
    }



     static class config{


        @Bean
        @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
        public Bean1 bean1(){return new Bean1();}
    }

    static class Bean1{
        public void foo(){
            System.out.println("bean1 foo");
        }
    }

    @Aspect
    static class MyAdvice{

        @Around("execution(* foo())")
        public Object around(ProceedingJoinPoint pjp) throws Throwable {
            System.out.println("before");
            return pjp.proceed();
        }
    }
}
